const express = require('express');
// const path = require('path');
const proxy = require('http-proxy-middleware');
const app = express();
module.exports = function(serverUrl, port) {
  // 设置静态文件路径
  // app.use(express.static(path.join(__dirname, './')));
  // 解决跨域问题
  app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header(
      'Access-Control-Allow-Headers',
      'Content-Type,Content-Length, Authorization, Accept,X-Requested-With'
    );
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
    if (req.method == 'OPTIONS') {
      res.sendStatus(200);
    } else {
      next();
    }
  });

  let options = {
    target: serverUrl,
    changeOrigin: true,
    logLevel: 'debug',
    onProxyReq(proxyReq, req, res) {
      console.log("method", req.method);
    }
  };

  const exampleProxy = proxy(options);
  app.use('/', exampleProxy); //  ‘/’ 表示对所有请求代理

  app.listen(port, function() {
    console.log('server is running at port ' + port);
  });


}